﻿Imports DataModel

Public Class Import
  Public Sub ImportSessions(ByVal conference As ConferenceListModel.Conference, ByVal verifyLinks As Boolean)
    Try
      BuildListAgilityPack("https://content.visitmix.com/2009/api/sessions.aspx", conference, verifyLinks)

    Catch ex As Exception

    End Try

  End Sub

  Private Sub BuildListAgilityPack(ByVal CatalogURL As String, ByVal conference As ConferenceListModel.Conference, ByVal verifyLinks As Boolean)
    Dim htmWeb As New HtmlAgilityPack.HtmlWeb

    Dim htmDoc As HtmlAgilityPack.HtmlDocument = htmWeb.Load(CatalogURL)

    'Dim links As HtmlAgilityPack.HtmlNodeCollection = htmDoc.DocumentNode.SelectNodes("//div[@class='summary']")

    Dim links As HtmlAgilityPack.HtmlNodeCollection = htmDoc.DocumentNode.SelectNodes("//div[@class='catalog']")

    For Each link As HtmlAgilityPack.HtmlNode In links

      Dim summary = link.SelectSingleNode(".//div[@class='summary']")

      Dim title = summary.ChildNodes(0).InnerText.Trim
      Dim code = link.SelectSingleNode(".//span[@class='code']").InnerText.Replace("MIX09-", "").Trim

      If code.Length = 0 Then
        code = summary.ChildNodes(1).InnerText.Replace("MIX09-", "")
      End If
      If code.Trim.Length = 0 AndAlso link.ChildNodes.Count = 3 Then
        code = summary.ChildNodes(2).InnerText.Replace("MIX09-", "")
      End If

      If title.Trim.Length = 0 Then
        title = "Title Unknown " & code
      End If
      Dim description = link.SelectSingleNode(".//div[@class='description']").InnerText.Trim

      Debug.Assert(code.Trim.Length > 0)
      Debug.Assert(title.Trim.Length > 0)

      Dim s As New ConferenceListModel.Session
      s.SessionID = Guid.NewGuid
      s.ConferenceID = conference.ConferenceID
      s.SessionTitle = title
      s.SessionCode = code
      s.SessionDTTM = CDate(link.SelectSingleNode(".//abbr[@class='dtstart']").Attributes("title").Value.Replace(":00000", ""))
      s.SessionDescription = Left(description, 2000)
      s.SessionURL = conference.RootSessionURL & code
      s.WMVHighMediaURL = Helpers.Resources.GetResourceURL(conference.RootVideoURL & "wmv-hq/", code, "wmv", "WMV-HQ", verifyLinks)
      s.WMVMediaURL = Helpers.Resources.GetResourceURL(conference.RootVideoURL & "wmv/", code, "wmv", "WMV", verifyLinks)
      s.MP4MediaURL = Helpers.Resources.GetResourceURL(conference.RootVideoURL & "mp4/", code, "mp4", "MP4", verifyLinks)
      s.PowerPointMediaURL = Helpers.Resources.GetResourceURL(conference.RootVideoURL & "pptx/", code, "pptx", "PPTX", verifyLinks)

      s.ZuneMediaURL = Helpers.Resources.GetResourceURL(conference.RootVideoURL & "zune/", code, "wmv", "Zune", verifyLinks)
      s.SourceCodeURL = Helpers.Resources.GetResourceURL(conference.RootVideoURL & "zip/", code, "zip", "ZIP", verifyLinks)

      conference.Sessions.Add(s)

    Next

  End Sub

End Class
